package gyb.rasa.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import gyb.rasa.entity.Ip;
import gyb.rasa.mapper.IpMapper;
import gyb.rasa.service.IpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 郜宇博
 * @since 2023-05-17
 */
@Service
public class IpServiceImpl extends ServiceImpl<IpMapper, Ip> implements IpService {

    /**
     * 分页获取IP
     *
     * @param current 当前页
     * @param limit   最大数量
     * @return
     */
    @Override
    public IPage<Ip> getIpByPage(Long current, Long limit) {
        IPage<Ip> ipPage = new Page<>(current, limit);
        IPage<Ip> page = this.page(ipPage);
        return page;
    }

    /**
     * 添加ip对象
     *
     * @param ipAdress
     * @return
     */
    @Override
    public int addIpUnique(String ipAdress) {
        List<Ip> list = this.list();
        for (Ip ip : list) {
            if (ip.getIp().equals(ipAdress)){
                return ip.getId();
            }
        }
        Ip ip = new Ip();
        ip.setIp(ipAdress);
        this.save(ip);
        return ip.getId();
    }
}
